<?php

namespace APP\Ctrl;
use CMS\Ctrl as Base;
use CMS\Helper as Helper;

class Status extends Base {
	// construct
	public function run() {
		// manage uri paths
		if(count($this->uri) > 0) {
			@list($func) = $this->uri;
			$this->$func();
		} else $this->index();
	}

	// indexxx
	public function index() {
		$this->view->func = 'index';

		// set title
		$this->view->title = $this->settings['blizz_guild'] . ' - Realm Status';

		// get realm status
		$realm = $this->db->querySingle("SELECT value FROM blizzard WHERE api = 'realm'");
		if(is_null($realm))
			die(new Error());

		// data
		$this->view->realm = unserialize($realm)->realms[0];

		// realm online
		$this->view->realm->status = $this->view->realm->status ? 1 : 0;
		$this->view->realm->queue = $this->view->realm->queue ? 1 : 0;
		$this->view->online = array(
        	'0'  => array('0'=> '<span class="red">Offline</span>', '1'=> '<span class="red">Offline</span>'),
        	'1'  => array('0'=> '<span class="green">Online and no queue</span>', '1'=> '<span class="orange">Online but there is a queue</span>')
		);

		// current time
		$date = new \DateTime("now", new \DateTimeZone($this->view->realm->timezone));
		$this->view->time = $date->format(DATE_RFC850);

		// zone status
		$this->view->status = array(
        	'-1' => array('0'=> '<span class="grey">Unknown</span>', '1'=> '<span class="grey">Unknown</span>'),
        	'0'  => array('0'=> '<span class="Alliance">Alliance controlled</span>', '1'=> '<span class="Horde">Horde controlled</span>'),
        	'1'  => array('0'=> '<span class="Horde">Horde attacking</span>', '1'=> '<span class="Alliance">Alliance attacking</span>'),
        	'2'  => array('0'=> '<span class="Horde">Horde attacking</span>', '1'=> '<span class="Alliance">Alliance attacking</span>'),
        	'3'  => array('0'=> '<span class="Alliance">Alliance controlled</span>', '1'=> '<span class="Horde">Horde controlled</span>')
		);

		// timers
		$this->view->wg_timer = Helper::format_time(floor($this->view->realm->wintergrasp->next / 1000));
		$this->view->tb_timer = Helper::format_time(floor($this->view->realm->{'tol-barad'}->next / 1000));

		// set path
		$this->view->path = array(
			'func' => $this->view->func
		);
	}
}